package com.map;

import java.awt.*;
import java.io.File;
import javax.swing.*;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

public class OpenFile extends JFrame 
{
	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	static JTextField filePath = null;
	private JButton openFile = null;
	private JButton playButton = null;
	static File selectedFile;
	
	private JTextField getJTextField()
	{
		if (filePath == null) 
		{
			filePath = new JTextField();
			filePath.setPreferredSize(new Dimension(200, 22));
			filePath.setEditable(false);
		}
		return filePath;
	}
	
	// 获取选择文件按钮的方法
	private JButton getOpenFile() 
	{
		if (openFile == null)
		{
			openFile = new JButton();
			openFile.setText("选择文件");
			openFile.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser fileChooser = new JFileChooser();
					fileChooser.showOpenDialog(OpenFile.this);
					selectedFile = fileChooser.getSelectedFile();
					filePath.setText(selectedFile.getAbsolutePath());
				}
			});
		}
		return openFile;
	}
	
	// 获取播放按钮的方法
	private JButton getPlayButton() 
	{
		if (playButton == null)
		{
			playButton = new JButton();
			playButton.setText("查看图片");
			playButton.addActionListener(new java.awt.event.ActionListener() {
						@SuppressWarnings("deprecation")
						public void actionPerformed(java.awt.event.ActionEvent e) {
							MapFrame ht=new MapFrame();
							ht.show();
							
						}
					});
		}
		return playButton;
	}
	
	public static void main(String[] args)
	{
		OpenFile thisClass = new OpenFile();
		thisClass.setVisible(true);
	}
	
	public OpenFile()
	{
		super();
		initialize();
	}
	
	// 初始化方法
	private void initialize()
	{
		this.setSize(408, 79);
		this.setContentPane(getJContentPane());
        Rectangle bounds = Display.getDefault().getPrimaryMonitor().getBounds();
        java.awt.Rectangle rect = this.getBounds();
        int x = bounds.x + (bounds.width - rect.width) / 2;
        int y = bounds.y + (bounds.height - rect.height) / 2;
        this.setLocation(x, y);
		this.setTitle("配置图片");
	}
	
	// 初始化内容面板的方法
	private JPanel getJContentPane()
	{
		if (jContentPane == null) 
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(new FlowLayout());
			jContentPane.add(getJTextField(), null);
			jContentPane.add(getOpenFile(), null);
			jContentPane.add(getPlayButton(), null);
		}
		return jContentPane;
	}
}